// 购物车路由
const express = require("express");
// 引入connect-multiparty中间件
var multipart = require("connect-multiparty");
// 创建中间件对象
var multipartMiddleware = multipart();
// 引入数据库配置
const db = require("../mysql");
// 创建路由对象
const shopcart = express.Router();

// 获取购物车信息
shopcart.get('/getShopCart/:username', multipartMiddleware, (req, res) => {
    // console.log(req.params)
    const sqlStr = `select book_cover,book_name,book_seller,book_price_yuanjia,book_price_xianjia,book_num,shopcart_book_id,num,ischeck FROM shopcart,books,users WHERE shopcart.shopcart_book_id=books.book_id AND shopcart.user_username=users.username AND shopcart.user_username='${req.params.username}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '获取购物车数据失败' });
        res.json({ code: 200, data: results });
    });
})

// 查询用户的购物车的某个商品
shopcart.post('/findShopCart', multipartMiddleware, (req, res) => {
    // console.log(req.body)
    const sqlStr = `select book_cover,book_name,book_seller,book_price_yuanjia,book_price_xianjia,book_num,shopcart_book_id,num,ischeck from shopcart,books,users where shopcart.shopcart_book_id=books.book_id AND shopcart.user_username=users.username and shopcart.shopcart_book_id='${req.body.book_id}' and shopcart.user_username='${req.body.username}'`
    db.query(sqlStr, (err, results) => {
        if (err) {
            return res.json({ code: 404, message: err })
        }
        else {
            // console.log(results[0])
            if (results[0]===undefined) return res.json({ code: 400, message: '不存在该图书' })
            return res.json({ code: 200, message: '已存在该图书', data: results[0] })
        }
    })
})

// 新插入购物车信息
shopcart.post('/addShopCart', multipartMiddleware, (req, res) => {
    // console.log(req.body)
    const sqlStr = `insert into shopcart (shopcart_book_id,user_username,num)  values ('${req.body.book_id}','${req.body.username}','1')`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '加入购物车失败' });
        return res.json({ code: 200, message: '加入购物车成功' })
    })
})

// 更新购物车商品数量
shopcart.put('/updateShopCart', multipartMiddleware, (req, res) => {
    const sqlStr = `update shopcart set num='${req.body.num}' where user_username='${req.body.username}' and shopcart_book_id='${req.body.book_id}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '修改购物车商品数量失败' });
        return res.json({ code: 200, message: '修改购物车商品数量成功' })
    })
})

// 更新选中复选框
shopcart.put('/checkShopCart', multipartMiddleware, (req, res) => {
    console.log(req.body)
    const sqlStr =`update shopcart set ischeck='${req.body.ischeck}' where user_username='${req.body.username}' and shopcart_book_id='${req.body.book_id}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: '未更新选中' });
        return res.json({ code: 200, message: '已更新选中' })
    })
})

// 删除购物车信息
shopcart.post('/deleteShopCart', multipartMiddleware, (req, res) => {
    const sqlStr = `delete from shopcart where user_username='${req.body.username}' and shopcart_book_id = '${req.body.book_id}'`
    db.query(sqlStr, (err, results) => {
        if (err) return res.json({ code: 400, message: "删除购物车商品失败" });
        res.json({ code: 200, message: "删除购物车商品成功" });
      });
})
module.exports = shopcart
